package xyz.liut.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

import xyz.liut.liutlibraries.ScreenUtils;


/**
 * 作者：LL
 * 邮箱：596828972@qq.com
 * 时间：2016/1/5
 */
public class ScaleScreenImageView extends ImageView {

    public ScaleScreenImageView(Context context) {
        super(context);
    }

    public ScaleScreenImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScaleScreenImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        if (bm == null){
            return;
        }
        if (getWidth() > 0) {
            ViewGroup.LayoutParams params = getLayoutParams();
            float es = (float) ScreenUtils.getScreenWidth(getContext()) / (float) bm.getWidth();
            params.height = (int) (bm.getHeight() * es);;
            setLayoutParams(params);
        }
    }
}
